package com.rokyinfo.pdm.controller.v1;

import com.rokyinfo.pdm.controller.BasicPath;
import com.rokyinfo.pdm.entity.DFirm;
import com.rokyinfo.pdm.service.IDFirmService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 车厂信息表 前端控制器
 * </p>
 *
 * @author YuanZhiJian
 * @since 2017-01-21
 */
@Api(value = "/firms", tags = "Firm", description = "厂商信息")
@RestController
@RequestMapping(value = BasicPath.V1 + "/firms")
public class DFirmController {

    @Autowired
    private IDFirmService mIDFirmService;

    @ApiOperation(value = "获取厂商列表", notes = "")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseEntity<List<DFirm>> getFirmList() throws Exception {
        // 还可以通过@RequestParam从页面中传递参数来进行查询条件或者翻页信息的传递
        return ResponseEntity.ok(mIDFirmService.selectByMap(null));
    }

    @ApiOperation(value = "新建厂商", notes = "根据DFirm对象创建厂商")
    @ApiImplicitParam(name = "dFirm", value = "厂商实体DFirm", required = true, dataType = "DFirm")
    @RequestMapping(value = "/", method = RequestMethod.POST)
    public ResponseEntity postFirm(@RequestBody DFirm dFirm) {
        // 除了@ModelAttribute绑定参数之外，还可以通过@RequestParam从页面中传递参数
        if (mIDFirmService.insert(dFirm)) {
            return new ResponseEntity(HttpStatus.CREATED);
        } else {
            return ResponseEntity.unprocessableEntity().build();
        }
    }


    @ApiOperation(value = "获取厂商信息", notes = "根据url的id来获取厂商信息")
    @ApiImplicitParam(name = "id", value = "厂商ID", required = true, dataType = "Long", paramType = "path")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ResponseEntity<DFirm> getFirm(@PathVariable Long id) {
        DFirm mDFirm = mIDFirmService.selectById(id);
        if (mDFirm != null) {
            return ResponseEntity.ok(mDFirm);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @ApiOperation(value = "更新厂商信息", notes = "根据url的id来指定更新对象，并根据传过来的dFirm信息来更新厂商信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "厂商ID", required = true, dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "dFirm", value = "厂商实体DFirm", required = true, dataType = "DFirm")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public ResponseEntity putFirm(@PathVariable Long id, @RequestBody DFirm dFirm) {
        dFirm.setFirmId(id);
        if (mIDFirmService.updateById(dFirm)) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.unprocessableEntity().build();
        }
    }


    @ApiOperation(value = "删除厂商", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "厂商ID", required = true, dataType = "Long", paramType = "path")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseEntity deleteFirm(@PathVariable Long id) {
        if (mIDFirmService.deleteById(id)) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }

}
